package com.usian.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.mapper.TbContentCategoryMapper;
import com.usian.mapper.TbContentMapper;
import com.usian.pojo.TbContent;
import com.usian.pojo.TbContentCategory;
import com.usian.pojo.TbContentCategoryExample;
import com.usian.pojo.TbContentExample;
import com.usian.redis.RedisClient;
import com.usian.service.ContentCategoryService;
import com.usian.utils.AdNode;
import com.usian.utils.PageResult;
import com.usian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {

    @Autowired
    private TbContentCategoryMapper contentCategoryMapper;

    @Autowired
    private TbContentMapper contentMapper;

    @Autowired
    RedisClient redisClient;

    @Value("${PORTAL_AD_KEY}")
    private String PORTAL_AD_KEY;

    @Override
    public List<TbContentCategory> selectContentCategoryByParentId(Long id) {
        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(id);
        criteria.andStatusEqualTo(1);
        List<TbContentCategory> list = contentCategoryMapper.selectByExample(example);
        return list;

    }

    @Override
    public Integer insertContentCategory(Integer parentId, String name) {
        TbContentCategory tbContentCategory = new TbContentCategory();
        tbContentCategory.setName(name);
        tbContentCategory.setParentId(Long.valueOf(parentId));
        tbContentCategory.setCreated(new Date());
        tbContentCategory.setUpdated(new Date());
        tbContentCategory.setIsParent(false);
        tbContentCategory.setSortOrder(1);
        tbContentCategory.setStatus(1);
        int contentCategoryNum = contentCategoryMapper.insert(tbContentCategory);
        TbContentCategory contentCategory = contentCategoryMapper.selectByPrimaryKey(Long.valueOf(parentId));
        if (!contentCategory.getIsParent()) {
            contentCategory.setIsParent(true);
            contentCategory.setUpdated(new Date());
            contentCategoryMapper.updateByPrimaryKey(contentCategory);
        }
        return contentCategoryNum;
    }

    @Override
    public Integer deleteContentCategoryById(Long categoryId) {
        TbContentCategory contentCategory = contentCategoryMapper.selectByPrimaryKey(categoryId);
        if (contentCategory.getIsParent() == true) {
            return 0;
        }

        contentCategory.setStatus(2);
        Integer num = contentCategoryMapper.updateByPrimaryKeySelective(contentCategory);

        /*contentCategoryMapper.deleteByPrimaryKey(id);*/
        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(contentCategory.getParentId());
        List<TbContentCategory> list = contentCategoryMapper.selectByExample(example);
        if (list.size() == 0) {
            /*TbContentCategory category = contentCategoryMapper.selectByPrimaryKey(contentCategory.getParentId());*/
            TbContentCategory tbContentCategory = new TbContentCategory();
            tbContentCategory.setIsParent(false);
            tbContentCategory.setUpdated(new Date());
            contentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory);
        }
        return num;
    }

    @Override
    public Integer updateContentCategory(Long id, String name) {
        TbContentCategory tbContentCategory = contentCategoryMapper.selectByPrimaryKey(id);
        Date date = new Date();
        tbContentCategory.setName(name);
        tbContentCategory.setUpdated(date);
        Integer num = contentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory);
        return num;
    }

    @Override
    public PageResult selectTbContentAllByCategoryId(Integer page, Integer rows, Long categoryId) {
        PageHelper.startPage(page, rows);
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        List<TbContent> list = contentMapper.selectByExample(example);
        PageInfo<TbContent> pageInfo = new PageInfo<>(list);
        return new PageResult(pageInfo.getPageNum(), pageInfo.getPages(), pageInfo.getList());
    }

    /*@Override
    public List<TbContent> selectTbContentAllByCategoryId(Long categoryId) {
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        List<TbContent> list = contentMapper.selectByExample(example);
        return list;
    }*/

    @Override
    public Integer insertTbContent(TbContent tbContent) {
        tbContent.setCreated(new Date());
        tbContent.setUpdated(new Date());
        Integer num = contentMapper.insertSelective(tbContent);
        redisClient.del(PORTAL_AD_KEY);
        return num;
    }

    @Override
    public Integer deleteContentByIds(Long id) {
        Integer num = contentMapper.deleteByPrimaryKey(id);
        redisClient.del(PORTAL_AD_KEY);
        return num;
    }


    @Value("${AD_CATEGORY_ID}")
    private Long AD_CATEGORY_ID;
    @Value("${AD_HEIGHT}")
    private Integer AD_HEIGHT;
    @Value("${AD_WIDTH}")
    private Integer AD_WIDTH;
    @Value("${AD_HEIGHTB}")
    private Integer AD_HEIGHTB;
    @Value("${AD_WIDTHB}")
    private Integer AD_WIDTHB;

    @Override
    public List<AdNode> selectFrontendContentByAD() {
        ArrayList<AdNode> adNodeListRedis = (ArrayList<AdNode>) redisClient.hget("PORTAL_AD_KEY", AD_CATEGORY_ID.toString());
        if (adNodeListRedis != null) {
            return adNodeListRedis;
        }
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(AD_CATEGORY_ID);
        List<TbContent> tbContentList = contentMapper.selectByExample(example);

        ArrayList<AdNode> adNodeList = new ArrayList<>();
        for (TbContent tbContent : tbContentList) {
            AdNode adNode = new AdNode();
            adNode.setSrc(tbContent.getPic());
            adNode.setSrcB(tbContent.getPic2());
            adNode.setHref(tbContent.getUrl());
            adNode.setHeight(AD_HEIGHT);
            adNode.setWidth(AD_WIDTH);
            adNode.setHeightB(AD_HEIGHTB);
            adNode.setWidthB(AD_WIDTHB);
            adNodeList.add(adNode);
        }
        redisClient.hset("PORTAL_AD_KEY", AD_CATEGORY_ID.toString(), adNodeList);
        return adNodeList;
    }
}
